package com.opo5.logisticsmanagement.controller.Interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * 拦截器
 */
@Configuration
public class Interceptor01 extends WebMvcConfigurationSupport implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        HttpSession session = request.getSession(false);
        if (session==null){
            System.out.println("该用户未登录...");
            return false;
        }
        String nickname = (String)session.getAttribute("nickname");
//        String user_role = session.getAttribute("user_role").toString();
//        System.out.println("------------------------------------------------------");
//        System.out.println(nickname);
//        System.out.println(session.getId());
//        System.out.println(user_role);
        return nickname != null && !nickname.equals("");
    }
    /**
     *
     * @param registry 配置拦截路径
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        String[] interceptorPath = new String[]{
                "/**",
        };
        registry.addInterceptor(this)
                .addPathPatterns(interceptorPath)//拦截地址
                .excludePathPatterns("/userLogin");//不拦截地址
    }
}
